package cn.bx.bid.ctrl;

import cn.bx.bid.entity.Page;
import cn.bx.bid.entity.Professor;
import cn.bx.bid.service.ProfessorService;
import cn.bx.bid.util.AjaxResult;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;


@Controller
public class ProfessorController  {
    @Resource
    ProfessorService professorService;

    //专家列表
    @GetMapping(value = "professorSearch",produces = "application/json;charset=utf-8")
    @ResponseBody
    public Page<Professor> professorpaging(@RequestParam(name = "page",defaultValue = "1")int pageNo, @RequestParam(name = "limit",defaultValue = "10") int limit, @RequestParam(name = "professorname",required = false)String professorname, @RequestParam(name = "mobliephone",required = false) String mobliephone, @RequestParam(name = "identitynumber",required = false) String identitynumber) {

        Page<Professor> p = this.professorService.paging(pageNo,limit,professorname,mobliephone,identitynumber);
        return p;
    }

    //添加专家
    @PostMapping(value = "professorAdd", produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult addProfessor(@ModelAttribute("u") Professor professor) {
        System.out.println(">>>>>>>>>>>>"+professor);
        int r = this.professorService.add(professor);
        if (r>0){

            return AjaxResult.success("success");
        }
        return AjaxResult.error("error");
    }


    //获取值
    @RequestMapping(value = "professorGet",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult professorGet(@RequestParam(name="id")long id) {
        Professor professor= this.professorService.get(id);
        return AjaxResult.success("msg",professor);
    }

    //修改
    @RequestMapping(value = "professorupdate",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult  professorupdate(@ModelAttribute("p") Professor professor)
    {
        int r=this.professorService.update(professor);
        if (r>0){
            return AjaxResult.success("success");
        }
        return AjaxResult.error();
    }
    //删除
    @RequestMapping(value = "delprofessor",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult delprofessor(@RequestParam(name = "id",required = false) long id){
        int i=this.professorService.del(id);
        if (i>0){
            return AjaxResult.success("success");
        }
        return AjaxResult.error();
    }
    //批量删除

    @RequestMapping(value = "ProfessorbatchDel",produces = "application/json;charset=utf-8")
    @ResponseBody
    public AjaxResult ProfessorbatchDel(@RequestParam("ids[]") long[] ids) {

        int r = professorService.ProfessorbatchDel(ids);
        if (r>0){
            return AjaxResult.success("success");
        }
        return AjaxResult.error("error");
    }
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
        // true:允许输入空值，false:不能为空值
    }
}
